package pl.enigmatic.parser.xml;

import org.w3c.dom.Document;

import pl.enigmatic.util.ResourceLocator;
import pl.enigmatic.util.XML;


public interface XMLResourceLocator {

	public Document getXML(String path);

	public static class BasicImpl implements XMLResourceLocator {

		private static final String EXT = ".xml";

		private static class SimpleResourceLocator extends ResourceLocator {

			public SimpleResourceLocator() {
				super(BasicImpl.class);
			}
		}

		protected final ResourceLocator locator;

		public BasicImpl(final ResourceLocator locator) {
			this.locator = locator;
		}

		public BasicImpl() {
			this(new SimpleResourceLocator());
		}

		@Override
		public final Document getXML(final String path) {
			return XML.parseStream(locator.getStream(path.length() > EXT.length() && path.lastIndexOf(EXT) == path.length() - EXT.length() ? path
					: path + EXT));
		}
	}

}
